/*
 * File: Fu_Foc.c
 *
 * Code generated for Simulink model 'Fu_Foc'.
 *
 * Model version                  : 1.373
 * Simulink Coder version         : 8.2 (R2012a) 29-Dec-2011
 * TLC version                    : 8.2 (Dec 29 2011)
 * C/C++ source code generated on : Sun Jun 16 10:53:06 2013
 *
 * Target selection: ert.tlc
 * Embedded hardware selection: ARM Compatible->ARM Cortex
 * Code generation objectives: Unspecified
 * Validation result: Not run
 */

#include "Fu_Foc.h"
#include "Fu_Foc_private.h"

/* Exported block signals */
int16_T Foc_CurrAlpha;                 /* '<S2>/Gain2' */
int16_T Foc_CurrBeta;                  /* '<S2>/Gain3' */
int16_T Foc_CurrD;                     /* '<S3>/Gain' */
int16_T Foc_VoltReqD;                  /* '<S4>/Switch3' */
int16_T Foc_CurrQ;                     /* '<S3>/Gain1' */
int16_T Foc_VoltReqQ;                  /* '<S4>/Switch2' */
int16_T Foc_VoltReqAlpha;              /* '<S5>/Add' */
int16_T Foc_VoltReqBeta;               /* '<S5>/Add1' */
int16_T Foc_PwmHelperX;                /* '<S7>/Product' */
int16_T Foc_PwmHelperY;                /* '<S7>/Product3' */
int16_T Foc_PwmHelperZ;                /* '<S7>/Product4' */
int16_T Foc_PwmCntr1;                  /* '<S6>/Raumzeigermodulation' */
int16_T Foc_PwmCntr2;                  /* '<S6>/Raumzeigermodulation' */
int16_T Foc_PwmCntr3;                  /* '<S6>/Raumzeigermodulation' */
int16_T Foc_CosTheta;                  /* '<S1>/cos-Table' */
int16_T Foc_SinTheta;                  /* '<S1>/sin-Table' */

/* Block states (auto storage) */
D_Work_Fu_Foc Fu_Foc_DWork;

/* Real-time model */
RT_MODEL_Fu_Foc Fu_Foc_M_;
RT_MODEL_Fu_Foc *const Fu_Foc_M = &Fu_Foc_M_;

/* Model step function */
void Fu_Foc_step(void)
{
  uint32_T fractionTmp_0d;
  uint32_T rtb_Prelookup_o1;
  boolean_T rtb_RelationalOperator_a;
  uint8_T rtb_CntrSection;
  uint32_T rtb_Prelookup_o2;
  int16_T rtb_Product3_m;
  int32_T rtb_Switch1;
  int32_T rtb_Switch;
  int32_T q;
  int32_T qY;
  int32_T tmp;
  uint16_T tmp_0;
  uint16_T tmp_1;

  /* PreLookup: '<S1>/Prelookup' incorporates:
   *  Inport: '<Root>/MoCoord_TrqReq3'
   */
  rtb_Prelookup_o1 = plook_u32u16u32n31_bincp_f(FuSpd_PosRot,
    Fu_Foc_ConstP.Prelookup_Breakpoint, 24U, &rtb_Prelookup_o2,
    &Fu_Foc_DWork.Prelookup_DWORK1);

  /* Interpolation_n-D: '<S1>/cos-Table' */
  fractionTmp_0d = rtb_Prelookup_o2;
  rtb_Switch1 = intrp1d_2xbbb9lhbt6vdc2pqb(rtb_Prelookup_o1, fractionTmp_0d,
    Fu_Foc_ConstP.cosTable_Table);
  Foc_CosTheta = (int16_T)(rtb_Switch1 >> 15);

  /* Gain: '<S2>/Gain2' incorporates:
   *  Gain: '<S2>/Gain'
   *  Gain: '<S2>/Gain1'
   *  Inport: '<Root>/Net_CurrPhasU'
   *  Inport: '<Root>/Net_CurrPhasV'
   *  Inport: '<Root>/Net_CurrPhasW'
   *  Sum: '<S2>/Add'
   */
  Foc_CurrAlpha = (int16_T)((int16_T)((int16_T)(Net_CurrPhasU - ((Net_CurrPhasV <<
    14) >> 15)) - ((Net_CurrPhasW << 14) >> 15)) * 21845 >> 15);

  /* Product: '<S3>/Product' */
  qY = Foc_CurrAlpha * Foc_CosTheta;

  /* Product: '<S2>/Product' incorporates:
   *  Inport: '<Root>/Net_CurrPhasV'
   */
  tmp = Net_CurrPhasV * 2165;
  tmp = (((tmp < 0 ? 4095 : 0) + tmp) >> 12) * 6711;

  /* Product: '<S2>/Product1' incorporates:
   *  Inport: '<Root>/Net_CurrPhasW'
   */
  q = Net_CurrPhasW * 2165;
  q = (((q < 0 ? 4095 : 0) + q) >> 12) * 6711;

  /* Gain: '<S2>/Gain3' incorporates:
   *  Product: '<S2>/Product'
   *  Product: '<S2>/Product1'
   *  Sum: '<S2>/Add1'
   */
  Foc_CurrBeta = (int16_T)((int16_T)((((tmp < 0 ? 4095 : 0) + tmp) >> 12) - (((q
    < 0 ? 4095 : 0) + q) >> 12)) * 21845 >> 15);

  /* Interpolation_n-D: '<S1>/sin-Table' */
  fractionTmp_0d = rtb_Prelookup_o2;
  rtb_Switch1 = intrp1d_2xbbb9lhbt6vdc2pqb(rtb_Prelookup_o1, fractionTmp_0d,
    Fu_Foc_ConstP.sinTable_Table);
  Foc_SinTheta = (int16_T)(rtb_Switch1 >> 15);

  /* Product: '<S3>/Product1' */
  tmp = Foc_CurrBeta * Foc_SinTheta;

  /* Gain: '<S3>/Gain' incorporates:
   *  Product: '<S3>/Product'
   *  Product: '<S3>/Product1'
   *  Sum: '<S3>/Add'
   */
  Foc_CurrD = (int16_T)((int16_T)((int16_T)(((qY < 0 ? 16383 : 0) + qY) >> 14) +
                         (int16_T)(((tmp < 0 ? 16383 : 0) + tmp) >> 14)) * 26755
                        >> 15);

  /* Sum: '<S4>/Subtract1' incorporates:
   *  Inport: '<Root>/MoCoord_TrqReq2'
   */
  rtb_Product3_m = (int16_T)(Emi_CurDReq - Foc_CurrD);

  /* RelationalOperator: '<S4>/Relational Operator' incorporates:
   *  Constant: '<S4>/Constant2'
   *  Inport: '<Root>/MoCoord_TrqReq5'
   */
  rtb_RelationalOperator_a = (Coord_States != 2);

  /* Switch: '<S4>/Switch1' incorporates:
   *  Constant: '<S4>/Zero1'
   *  Memory: '<S4>/Memory1'
   *  Product: '<S4>/Product4'
   *  Sum: '<S4>/Add3'
   */
  if (rtb_RelationalOperator_a) {
    rtb_Switch1 = 0;
  } else {
    rtb_Switch1 = rtb_Product3_m + Fu_Foc_DWork.Memory1_PreviousInput;
  }

  /* End of Switch: '<S4>/Switch1' */

  /* Switch: '<S4>/Switch3' incorporates:
   *  Constant: '<S4>/Constant1'
   *  Inport: '<Root>/MoCoord_TrqReq4'
   *  Product: '<S4>/Product3'
   */
  if (Coord_FlgRunReq) {
    /* Product: '<S4>/Product3' */
    qY = rtb_Product3_m * 25;
    qY = (((qY < 0 ? 63 : 0) + qY) >> 6) * 5243;
    Foc_VoltReqD = (int16_T)(((qY < 0 ? 4095 : 0) + qY) >> 12);
  } else {
    Foc_VoltReqD = 0;
  }

  /* End of Switch: '<S4>/Switch3' */

  /* Product: '<S3>/Product3' */
  qY = Foc_CosTheta * Foc_CurrBeta;

  /* Product: '<S3>/Product2' */
  tmp = Foc_CurrAlpha * Foc_SinTheta;

  /* Gain: '<S3>/Gain1' incorporates:
   *  Product: '<S3>/Product2'
   *  Product: '<S3>/Product3'
   *  Sum: '<S3>/Add1'
   */
  Foc_CurrQ = (int16_T)((int16_T)((int16_T)(((qY < 0 ? 16383 : 0) + qY) >> 14) -
                         (int16_T)(((tmp < 0 ? 16383 : 0) + tmp) >> 14)) * 26755
                        >> 15);

  /* Sum: '<S4>/Subtract' incorporates:
   *  Inport: '<Root>/MoCoord_TrqReq'
   */
  rtb_Product3_m = (int16_T)(Emi_CurQReq - Foc_CurrQ);

  /* Switch: '<S4>/Switch' incorporates:
   *  Constant: '<S4>/Zero'
   *  Memory: '<S4>/Memory'
   *  Product: '<S4>/Product1'
   *  Sum: '<S4>/Add1'
   */
  if (rtb_RelationalOperator_a) {
    rtb_Switch = 0;
  } else {
    rtb_Switch = rtb_Product3_m + Fu_Foc_DWork.Memory_PreviousInput;
  }

  /* End of Switch: '<S4>/Switch' */

  /* Switch: '<S4>/Switch2' incorporates:
   *  Constant: '<S4>/Constant'
   *  Inport: '<Root>/MoCoord_TrqReq4'
   *  Inport: '<Root>/Net_CurrPhasU1'
   *  Product: '<S4>/Product'
   *  Sum: '<S4>/Add'
   */
  if (Coord_FlgRunReq) {
    /* Product: '<S4>/Product' */
    qY = rtb_Product3_m * 15;
    qY = (((qY < 0 ? 63 : 0) + qY) >> 6) * 5243;
    Foc_VoltReqQ = (int16_T)((((qY < 0 ? 4095 : 0) + qY) >> 12) + Emi_CurrQReqFF);
  } else {
    Foc_VoltReqQ = 0;
  }

  /* End of Switch: '<S4>/Switch2' */

  /* Product: '<S5>/Product' */
  qY = Foc_CosTheta * Foc_VoltReqD;

  /* Product: '<S5>/Product1' */
  tmp = Foc_SinTheta * Foc_VoltReqQ;

  /* Sum: '<S5>/Add' incorporates:
   *  Product: '<S5>/Product'
   *  Product: '<S5>/Product1'
   */
  Foc_VoltReqAlpha = (int16_T)((int16_T)(((qY < 0 ? 16383 : 0) + qY) >> 14) -
    (int16_T)(((tmp < 0 ? 16383 : 0) + tmp) >> 14));

  /* Product: '<S5>/Product2' */
  qY = Foc_SinTheta * Foc_VoltReqD;

  /* Product: '<S5>/Product3' */
  tmp = Foc_VoltReqQ * Foc_CosTheta;

  /* Sum: '<S5>/Add1' incorporates:
   *  Product: '<S5>/Product2'
   *  Product: '<S5>/Product3'
   */
  Foc_VoltReqBeta = (int16_T)((int16_T)(((qY < 0 ? 16383 : 0) + qY) >> 14) +
    (int16_T)(((tmp < 0 ? 16383 : 0) + tmp) >> 14));

  /* Chart: '<S6>/SectionDetect' incorporates:
   *  Product: '<S6>/Divide'
   *  RelationalOperator: '<S6>/Relational Operator'
   */
  /* Gateway: Fu_Foc/Raumzeigermodulation/SectionDetect */
  /* During: Fu_Foc/Raumzeigermodulation/SectionDetect */
  /* Entry Internal: Fu_Foc/Raumzeigermodulation/SectionDetect */
  /* Transition: '<S9>:5' */
  if (Foc_VoltReqAlpha == 0) {
    /* Transition: '<S9>:7' */
    if (Foc_VoltReqBeta >= 0) {
      /* Transition: '<S9>:9' */
      rtb_CntrSection = 1U;
    } else {
      /* Transition: '<S9>:11' */
      /* Transition: '<S9>:13' */
      rtb_CntrSection = 4U;
    }
  } else {
    /* Abs: '<S6>/Abs1' */
    /* Transition: '<S9>:15' */
    if (Foc_VoltReqBeta < 0) {
      tmp_0 = (uint16_T)(int16_T)-Foc_VoltReqBeta;
    } else {
      tmp_0 = (uint16_T)Foc_VoltReqBeta;
    }

    /* End of Abs: '<S6>/Abs1' */

    /* Abs: '<S6>/Abs2' */
    if (Foc_VoltReqAlpha < 0) {
      tmp_1 = (uint16_T)(int16_T)-Foc_VoltReqAlpha;
    } else {
      tmp_1 = (uint16_T)Foc_VoltReqAlpha;
    }

    /* End of Abs: '<S6>/Abs2' */

    /* Product: '<S6>/Divide' */
    rtb_Prelookup_o1 = div_u32((uint32_T)tmp_0 << 13, (uint32_T)tmp_1);
    if (rtb_Prelookup_o1 > 65535U) {
      rtb_Prelookup_o1 = 65535U;
    }

    rtb_Prelookup_o1 = rtb_Prelookup_o1 * 625U >> 9;
    if (rtb_Prelookup_o1 > 65535U) {
      rtb_Prelookup_o1 = 65535U;
    }

    if ((int32_T)rtb_Prelookup_o1 <= 17321) {
      /* Transition: '<S9>:18' */
      if ((Foc_VoltReqAlpha >= 0) && (Foc_VoltReqBeta >= 0)) {
        /* Transition: '<S9>:26' */
        rtb_CntrSection = 0U;
      } else {
        /* Transition: '<S9>:22' */
        if ((Foc_VoltReqAlpha >= 0) && (Foc_VoltReqBeta < 0)) {
          /* Transition: '<S9>:28' */
          rtb_CntrSection = 5U;
        } else {
          /* Transition: '<S9>:34' */
          if ((Foc_VoltReqAlpha < 0) && (Foc_VoltReqBeta < 0)) {
            /* Transition: '<S9>:32' */
            rtb_CntrSection = 3U;
          } else {
            /* Transition: '<S9>:38' */
            /* Transition: '<S9>:37' */
            rtb_CntrSection = 2U;
          }
        }
      }
    } else {
      /* Transition: '<S9>:40' */
      if (Foc_VoltReqBeta >= 0) {
        /* Transition: '<S9>:42' */
        rtb_CntrSection = 1U;
      } else {
        /* Transition: '<S9>:44' */
        /* Transition: '<S9>:46' */
        rtb_CntrSection = 4U;
      }
    }
  }

  /* End of Chart: '<S6>/SectionDetect' */

  /* Product: '<S7>/Product' */
  qY = 4729 * Foc_VoltReqBeta;
  qY = ((qY < 0 ? 2047 : 0) + qY) >> 11;
  if (qY > 32767) {
    qY = 32767;
  } else {
    if (qY < -32768) {
      qY = -32768;
    }
  }

  qY *= 5243;
  qY = ((qY < 0 ? 4095 : 0) + qY) >> 12;
  if (qY > 32767) {
    qY = 32767;
  } else {
    if (qY < -32768) {
      qY = -32768;
    }
  }

  Foc_PwmHelperX = (int16_T)qY;

  /* End of Product: '<S7>/Product' */

  /* Product: '<S7>/Product2' */
  qY = Foc_VoltReqAlpha * 17321;
  qY = (int16_T)(((qY < 0 ? 16383 : 0) + qY) >> 14) * 6711;
  rtb_Product3_m = (int16_T)(((qY < 0 ? 4095 : 0) + qY) >> 12);

  /* Product: '<S7>/Product3' incorporates:
   *  Sum: '<S7>/Add'
   */
  qY = (int16_T)(Foc_VoltReqBeta + rtb_Product3_m) * 115;
  qY = (((qY < 0 ? 127 : 0) + qY) >> 7) * 6711;
  qY = ((qY < 0 ? 4095 : 0) + qY) >> 12;
  if (qY > 32767) {
    qY = 32767;
  } else {
    if (qY < -32768) {
      qY = -32768;
    }
  }

  Foc_PwmHelperY = (int16_T)qY;

  /* End of Product: '<S7>/Product3' */

  /* Product: '<S7>/Product4' incorporates:
   *  Sum: '<S7>/Add1'
   */
  qY = (int16_T)(Foc_VoltReqBeta - rtb_Product3_m) * 115;
  qY = (((qY < 0 ? 127 : 0) + qY) >> 7) * 6711;
  qY = ((qY < 0 ? 4095 : 0) + qY) >> 12;
  if (qY > 32767) {
    qY = 32767;
  } else {
    if (qY < -32768) {
      qY = -32768;
    }
  }

  Foc_PwmHelperZ = (int16_T)qY;

  /* End of Product: '<S7>/Product4' */

  /* Chart: '<S6>/Raumzeigermodulation' */
  /* Gateway: Fu_Foc/Raumzeigermodulation/Raumzeigermodulation */
  /* During: Fu_Foc/Raumzeigermodulation/Raumzeigermodulation */
  /* Entry Internal: Fu_Foc/Raumzeigermodulation/Raumzeigermodulation */
  /* Transition: '<S8>:14' */
  if (rtb_CntrSection == 0) {
    /* Transition: '<S8>:25' */
    /* Transition: '<S8>:30' */
    q = 4096 + Foc_PwmHelperZ;
    qY = q - Foc_PwmHelperX;
    if ((q < 0) && ((Foc_PwmHelperX >= 0) && (qY >= 0))) {
      qY = MIN_int32_T;
    } else {
      if ((q >= 0) && ((Foc_PwmHelperX < 0) && (qY < 0))) {
        qY = MAX_int32_T;
      }
    }

    qY = div_s32_SR(qY, 1, 2U);
    if (qY > 16383) {
      rtb_Product3_m = MAX_int16_T;
    } else if (qY <= -16384) {
      rtb_Product3_m = MIN_int16_T;
    } else {
      rtb_Product3_m = (int16_T)(qY << 1);
    }

    qY = rtb_Product3_m - Foc_PwmHelperZ;
    if (qY > 32767) {
      qY = 32767;
    } else {
      if (qY < -32768) {
        qY = -32768;
      }
    }

    Foc_PwmCntr1 = rtb_Product3_m;
    Foc_PwmCntr2 = (int16_T)qY;
    qY = (int16_T)qY + Foc_PwmHelperX;
    if (qY > 32767) {
      qY = 32767;
    } else {
      if (qY < -32768) {
        qY = -32768;
      }
    }

    Foc_PwmCntr3 = (int16_T)qY;
  } else {
    /* Transition: '<S8>:35' */
    if (rtb_CntrSection == 1) {
      /* Transition: '<S8>:38' */
      /* Transition: '<S8>:39' */
      q = 4096 - Foc_PwmHelperZ;
      qY = q - Foc_PwmHelperY;
      if ((q < 0) && ((Foc_PwmHelperY >= 0) && (qY >= 0))) {
        qY = MIN_int32_T;
      } else {
        if ((q >= 0) && ((Foc_PwmHelperY < 0) && (qY < 0))) {
          qY = MAX_int32_T;
        }
      }

      qY = div_s32_SR(qY, 1, 2U);
      if (qY > 16383) {
        rtb_Product3_m = MAX_int16_T;
      } else if (qY <= -16384) {
        rtb_Product3_m = MIN_int16_T;
      } else {
        rtb_Product3_m = (int16_T)(qY << 1);
      }

      qY = rtb_Product3_m + Foc_PwmHelperZ;
      if (qY > 32767) {
        qY = 32767;
      } else {
        if (qY < -32768) {
          qY = -32768;
        }
      }

      Foc_PwmCntr1 = (int16_T)qY;
      Foc_PwmCntr2 = rtb_Product3_m;
      qY = (int16_T)qY + Foc_PwmHelperY;
      if (qY > 32767) {
        qY = 32767;
      } else {
        if (qY < -32768) {
          qY = -32768;
        }
      }

      Foc_PwmCntr3 = (int16_T)qY;
    } else {
      /* Transition: '<S8>:45' */
      if (rtb_CntrSection == 2) {
        /* Transition: '<S8>:43' */
        /* Transition: '<S8>:44' */
        q = 4096 - Foc_PwmHelperX;
        qY = q + Foc_PwmHelperY;
        if ((q < 0) && ((Foc_PwmHelperY < 0) && (qY >= 0))) {
          qY = MIN_int32_T;
        } else {
          if ((q > 0) && ((Foc_PwmHelperY > 0) && (qY <= 0))) {
            qY = MAX_int32_T;
          }
        }

        qY = div_s32_SR(qY, 1, 2U);
        if (qY > 16383) {
          rtb_Product3_m = MAX_int16_T;
        } else if (qY <= -16384) {
          rtb_Product3_m = MIN_int16_T;
        } else {
          rtb_Product3_m = (int16_T)(qY << 1);
        }

        qY = rtb_Product3_m + Foc_PwmHelperX;
        if (qY > 32767) {
          qY = 32767;
        } else {
          if (qY < -32768) {
            qY = -32768;
          }
        }

        tmp = (int16_T)qY - Foc_PwmHelperY;
        if (tmp > 32767) {
          tmp = 32767;
        } else {
          if (tmp < -32768) {
            tmp = -32768;
          }
        }

        Foc_PwmCntr1 = (int16_T)tmp;
        Foc_PwmCntr2 = rtb_Product3_m;
        Foc_PwmCntr3 = (int16_T)qY;
      } else {
        /* Transition: '<S8>:51' */
        if (rtb_CntrSection == 3) {
          /* Transition: '<S8>:49' */
          /* Transition: '<S8>:50' */
          q = 4096 + Foc_PwmHelperX;
          qY = q - Foc_PwmHelperZ;
          if ((q < 0) && ((Foc_PwmHelperZ >= 0) && (qY >= 0))) {
            qY = MIN_int32_T;
          } else {
            if ((q >= 0) && ((Foc_PwmHelperZ < 0) && (qY < 0))) {
              qY = MAX_int32_T;
            }
          }

          qY = div_s32_SR(qY, 1, 2U);
          if (qY > 16383) {
            rtb_Product3_m = MAX_int16_T;
          } else if (qY <= -16384) {
            rtb_Product3_m = MIN_int16_T;
          } else {
            rtb_Product3_m = (int16_T)(qY << 1);
          }

          qY = rtb_Product3_m - Foc_PwmHelperX;
          if (qY > 32767) {
            qY = 32767;
          } else {
            if (qY < -32768) {
              qY = -32768;
            }
          }

          tmp = (int16_T)qY + Foc_PwmHelperZ;
          if (tmp > 32767) {
            tmp = 32767;
          } else {
            if (tmp < -32768) {
              tmp = -32768;
            }
          }

          Foc_PwmCntr1 = (int16_T)tmp;
          Foc_PwmCntr2 = (int16_T)qY;
          Foc_PwmCntr3 = rtb_Product3_m;
        } else {
          /* Transition: '<S8>:57' */
          if (rtb_CntrSection == 4) {
            /* Transition: '<S8>:55' */
            /* Transition: '<S8>:56' */
            q = 4096 + Foc_PwmHelperY;
            qY = q + Foc_PwmHelperZ;
            if ((q < 0) && ((Foc_PwmHelperZ < 0) && (qY >= 0))) {
              qY = MIN_int32_T;
            } else {
              if ((q > 0) && ((Foc_PwmHelperZ > 0) && (qY <= 0))) {
                qY = MAX_int32_T;
              }
            }

            qY = div_s32_SR(qY, 1, 2U);
            if (qY > 16383) {
              rtb_Product3_m = MAX_int16_T;
            } else if (qY <= -16384) {
              rtb_Product3_m = MIN_int16_T;
            } else {
              rtb_Product3_m = (int16_T)(qY << 1);
            }

            qY = rtb_Product3_m - Foc_PwmHelperY;
            if (qY > 32767) {
              qY = 32767;
            } else {
              if (qY < -32768) {
                qY = -32768;
              }
            }

            Foc_PwmCntr1 = (int16_T)qY;
            qY = (int16_T)qY - Foc_PwmHelperZ;
            if (qY > 32767) {
              qY = 32767;
            } else {
              if (qY < -32768) {
                qY = -32768;
              }
            }

            Foc_PwmCntr2 = (int16_T)qY;
            Foc_PwmCntr3 = rtb_Product3_m;
          } else {
            /* Transition: '<S8>:63' */
            /* Transition: '<S8>:61' */
            /* Transition: '<S8>:62' */
            q = 4096 - Foc_PwmHelperY;
            qY = q + Foc_PwmHelperX;
            if ((q < 0) && ((Foc_PwmHelperX < 0) && (qY >= 0))) {
              qY = MIN_int32_T;
            } else {
              if ((q > 0) && ((Foc_PwmHelperX > 0) && (qY <= 0))) {
                qY = MAX_int32_T;
              }
            }

            qY = div_s32_SR(qY, 1, 2U);
            if (qY > 16383) {
              rtb_Product3_m = MAX_int16_T;
            } else if (qY <= -16384) {
              rtb_Product3_m = MIN_int16_T;
            } else {
              rtb_Product3_m = (int16_T)(qY << 1);
            }

            qY = rtb_Product3_m + Foc_PwmHelperY;
            if (qY > 32767) {
              qY = 32767;
            } else {
              if (qY < -32768) {
                qY = -32768;
              }
            }

            Foc_PwmCntr1 = rtb_Product3_m;
            tmp = (int16_T)qY - Foc_PwmHelperX;
            if (tmp > 32767) {
              tmp = 32767;
            } else {
              if (tmp < -32768) {
                tmp = -32768;
              }
            }

            Foc_PwmCntr2 = (int16_T)tmp;
            Foc_PwmCntr3 = (int16_T)qY;
          }
        }
      }
    }
  }

  /* End of Chart: '<S6>/Raumzeigermodulation' */

  /* Update for Memory: '<S4>/Memory1' */
  Fu_Foc_DWork.Memory1_PreviousInput = rtb_Switch1;

  /* Update for Memory: '<S4>/Memory' */
  Fu_Foc_DWork.Memory_PreviousInput = rtb_Switch;
}

/* Model initialize function */
void Fu_Foc_initialize(void)
{
  /* Registration code */

  /* initialize error status */
  rtmSetErrorStatus(Fu_Foc_M, (NULL));

  /* block I/O */

  /* exported global signals */
  Foc_CurrAlpha = 0;
  Foc_CurrBeta = 0;
  Foc_CurrD = 0;
  Foc_VoltReqD = 0;
  Foc_CurrQ = 0;
  Foc_VoltReqQ = 0;
  Foc_VoltReqAlpha = 0;
  Foc_VoltReqBeta = 0;
  Foc_PwmHelperX = 0;
  Foc_PwmHelperY = 0;
  Foc_PwmHelperZ = 0;
  Foc_PwmCntr1 = 0;
  Foc_PwmCntr2 = 0;
  Foc_PwmCntr3 = 0;
  Foc_CosTheta = 0;
  Foc_SinTheta = 0;

  /* states (dwork) */
  (void) memset((void *)&Fu_Foc_DWork, 0,
                sizeof(D_Work_Fu_Foc));
}

/*
 * File trailer for generated code.
 *
 * [EOF]
 */
